import { getCampusSelector } from '@/api/campus/campus'
import request from '@/utils/request'
export default {
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      brandOptions: [],
      campusOptions: [],
      dataForm: {
        id: '',
        question: undefined,
        sourse: undefined,
        category: undefined,
        keywords: [],
        status: undefined,
        solutions: undefined,
        fqaSolutionsList: [],
        createTime: undefined,
        createUserName: undefined,
        updateTime: undefined,
        updateUserName: undefined
      },
      rules: {
        question: [
          {
            required: true,
            message: '请输入问题文本',
            trigger: 'blur'
          }
        ],
        sourse: [
          {
            required: true,
            message: '请输入来源',
            trigger: 'blur'
          }
        ],
        category: [
          {
            required: true,
            message: '请输入分类',
            trigger: 'change'
          }
        ],
        keywords: [
          {
            required: true,
            message: '请输入关键字',
            trigger: 'blur'
          }
        ]
        // status: [
        //   {
        //     required: true,
        //     message: '请输入状态',
        //     trigger: 'change'
        //   }
        // ]
      },
      categoryOptions: [],
      statusOptions: [],
      sourseOptions: []
    }
  },
  computed: {},
  watch: {},
  created() {
    // this.getcategoryOptions()
    // this.getstatusOptions()
  },
  mounted() {
    this.$store.dispatch('base/getDictionaryData', { sort: 'FqaCategory' }).then(res => {
      console.log(res, 'FqaCategory')
      this.categoryOptions = res
    })
    this.$store.dispatch('base/getDictionaryData', { sort: 'FqaSourse' }).then(res => {
      console.log(res, 'FqaSourse')
      this.sourseOptions = res
    })
    this.$store.dispatch('base/getDictionaryData', { sort: 'FqaStatus' }).then(res => {
      console.log(res, 'FqaStatus')
      this.statusOptions = res
    })
  },
  methods: {
    handleClose(tag) {
      this.dataForm.keywords.splice(this.dataForm.keywords.indexOf(tag), 1)
    },
    getKey(item, key) {
      if (this.dataForm.keywords.indexOf(item) < 0) {
        this.dataForm.keywords.push(item)
      }
    },
    handleChangeBrand(val) {
      this.dataForm.campusId = undefined
      getCampusSelector({ brand: val, status: 'normal' }).then(res => {
        this.campusOptions = res.data
      })
    },
    handleChangeCampus(value) {
      console.log(value)
    },

    goBack() {
      this.$emit('refresh')
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0
      this.visible = true
      this.isDetail = isDetail || false
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          request({
            url: '/api/extend/fqaentries/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            let keywords = res.data.keywords.split(',')
            this.dataForm = Object.assign(res.data, {
              keywords: keywords
            })
          })
        }
      })
    },
    dataFormSubmit() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          let data = Object.assign({}, this.dataForm)
          data = Object.assign(data, {
            // fqaSolutionsList: JSON.stringify(this.dataForm.fqaSolutionsList),
            keywords: this.dataForm.keywords.join(',')
          })
          if (!this.dataForm.id) {
            request({
              url: `/api/extend/fqaentries`,
              method: 'post',
              data: data
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  ;(this.visible = false), this.$emit('refresh', true)
                }
              })
            })
          } else {
            request({
              url: '/api/extend/fqaentries/' + this.dataForm.id,
              method: 'PUT',
              data: data
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refresh', true)
                }
              })
            })
          }
        }
      })
    },
    addHandleFqaSolutionsEntityList() {
      let item = {
        version: undefined,
        description: undefined
      }
      this.dataForm.fqaSolutionsList.push(item)
    },
    handleDelFqaSolutionsEntityList(index) {
      this.dataForm.fqaSolutionsList.splice(index, 1)
    }
  }
}
